package com.i2works.smartluolong.api.controller.fooddrug;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.i2works.smartluolong.api.controller.common.CommonItemController;
import com.i2works.smartluolong.utils.base.FooddrugConstant;
import com.i2works.smartluolong.utils.entity.R;
import com.i2works.smartluolong.utils.entity.Result;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.web.entity.flow.FlowInfoDO;
import com.i2works.smartluolong.web.entity.fooddrug.FooddrugApplyDO;
import com.i2works.smartluolong.web.entity.fooddrug.FooddrugApplyQuery;
import com.i2works.smartluolong.web.entity.fooddrug.FooddrugCancelDO;
import com.i2works.smartluolong.web.entity.fooddrug.FooddrugCancelQuery;
import com.i2works.smartluolong.web.service.flow.IFlowInfoService;
import com.i2works.smartluolong.web.service.flow.IFlowProcessService;
import com.i2works.smartluolong.web.service.flow.IFlowService;
import com.i2works.smartluolong.web.service.flow.IFlowTemplateService;
import com.i2works.smartluolong.web.service.fooddrug.IFooddrugApplyService;
import com.i2works.smartluolong.web.service.fooddrug.IFooddrugCancelService;
/**
 * 食药注销控制器
 *
 * @author Liu YaPeng
 * @date 2017/9/27 20:07
 */

@Controller
@RequestMapping("/fooddrugCancel")
public class FooddrugCancelController extends CommonItemController {

	private final IFooddrugApplyService fooddrugApplyService;

	private final IFooddrugCancelService fooddrugCancelService;
	
    private final IFlowInfoService flowInfoService;
    


	@Autowired
	public FooddrugCancelController(IFlowInfoService flowInfoService,IFlowTemplateService flowTemplateService, IFlowService flowService, IFlowProcessService flowProcessService,IFooddrugApplyService fooddrugApplyService,IFooddrugCancelService fooddrugCancelService) {
		super(flowTemplateService, flowService, flowProcessService);
		this.fooddrugApplyService=fooddrugApplyService;
		this.fooddrugCancelService=fooddrugCancelService;
		this.flowInfoService=flowInfoService;
	}


	
	@PostMapping("/base")
	@ResponseBody
	public Result base(Integer id) {
		Map<String,Object> map=new HashMap<String,Object>();
		try {
			FooddrugApplyQuery query=new FooddrugApplyQuery();
			query.setUserId(getUserId());
			query.setStatus(1);
			query.setApplyStatus(10);//展示申请通过的数据
			List<FooddrugApplyDO> list=fooddrugApplyService.queryList(query);
			map.put("list",list);
			if(id!=null) {
				FooddrugCancelQuery cancelQuery=new FooddrugCancelQuery();
				cancelQuery.setId(id);
				FooddrugCancelDO cancel=fooddrugCancelService.query(cancelQuery);
				map.put("cancelReason",cancel.getCancelReason());
			}
		} catch (ServiceException e) {
			return R.error();
		}
		return R.success(map);
	}

	@PostMapping("/saveApply")
	@ResponseBody
	public Result saveBase(FooddrugCancelDO fooddrugCancelDO) {
		try {
		fooddrugCancelDO.setCreateName(getUserName());
		fooddrugCancelDO.setUpdateName(getUserName());
		fooddrugCancelDO.setApplyStatus(1);
		fooddrugCancelService.save(fooddrugCancelDO);
		FlowInfoDO flowInfoDO=new FlowInfoDO();
		flowInfoDO.setUserId(fooddrugCancelDO.getUserId());
		flowInfoDO.setItemId(fooddrugCancelDO.getId());
		flowInfoDO.setItemName(fooddrugCancelDO.getCompanyName()+FooddrugConstant.CANCEL_TITLE);
		flowInfoDO.setItemStatus(1);
		flowInfoDO.setComplete(0);
		flowInfoDO.setItemType(FooddrugConstant.CANCEL_ITEM_TYPE);
		flowInfoDO.setCreateName(getUserName());
		flowInfoDO.setUpdateName(getUserName());
		flowInfoService.save(flowInfoDO);
		return R.success();
		} catch (ServiceException e) {
			e.printStackTrace();
		   return R.error();
		}
	}
	
	   @PostMapping("/view")
	   @ResponseBody
	    public Result view(Integer id) {
		   Map<String, Object> map =new HashMap<String, Object>();
	    	try {
	    		FooddrugCancelQuery query=new FooddrugCancelQuery();
	    		query.setId(id);
	    		FooddrugCancelDO fooddrugCancel=fooddrugCancelService.query(query);
	    		map.put("companyName",fooddrugCancel.getCompanyName());
	    		map.put("cancelReason", fooddrugCancel.getCancelReason());
	    	} catch (ServiceException e) {
	    		e.printStackTrace();
	    		return R.error();
	    	}
	    	return  R.success(map);
	    }
	  
	  /**
	     * 申请作废
	     * */
	    @PostMapping("/invalid/action")
	    @ResponseBody
	    public Result invalid(FlowInfoDO flowInfoDO) {
	    	try {
	    		flowInfoDO.setItemStatus(11);
	    		flowInfoService.update(flowInfoDO);
	    		FooddrugCancelDO cancelDO=new FooddrugCancelDO();
	    		cancelDO.setId(flowInfoDO.getItemId());
	    		cancelDO.setApplyStatus(11);
	    		fooddrugCancelService.update(cancelDO);
	    	} catch (ServiceException e) {
	    		e.printStackTrace();
	    		return R.error();
	    	}
	    	return R.success();
	    }

	    
}
